﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JDFoodMenu.Framework.Biz;
using System.Data;
using JDFoodMenu.WebTemplates;
using System.Text;
using JDFoodMenu.Model;
using JDFoodMenu.Entity;

namespace JDFoodMenu.Pages
{
    public partial class Show : PageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["CalendarDate"] != null && Request.QueryString["CalendarDate"] != string.Empty)
                    CalendarDate = DateTime.Parse(Request.QueryString["CalendarDate"]);

                ltlDuration.Text = StartDate.ToString("yyyy年MM月dd日") + " 到 " + StartDate.AddDays(4).ToString("yyyy年MM月dd日");
             
                lbName.Text = ddlRoom.SelectedItem.Text;

                BindData();

            }
        }

        public DateTime StartDate
        {
            get
            {
                if (ViewState["StartDate"] == null)
                {
                    ViewState["StartDate"] = BLCalendar.GetFirstDayOfWeek(CalendarDate);
                }
                return (DateTime)ViewState["StartDate"];
            }
            set
            {
                ViewState["StartDate"] = value;
            }
        }

        #region CalendarDate
        public DateTime CalendarDate
        {
            get
            {
                if (ViewState["CalendarDate"] == null)
                {
                    ViewState["CalendarDate"] = DateTime.Today;
                }
                return (DateTime)ViewState["CalendarDate"];
            }
            set
            {
                ViewState["CalendarDate"] = value;
            }
        }







        #endregion
        private string GetWeekDay(DayOfWeek dow)
        {
            string Temp = "";
            switch (dow)
            {
                case DayOfWeek.Sunday:
                    Temp = "星期天";
                    break;
                case DayOfWeek.Monday:
                    Temp = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    Temp = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    Temp = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    Temp = "星期四";
                    break;
                case DayOfWeek.Friday:
                    Temp = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    Temp = "星期六";
                    break;
            }
            return Temp;
        }
        private void BindData()
        {
            InitData();
            DataTable dt = new DataTable();
            dt.Columns.Add("day", typeof(string));
            dt.Columns.Add("time", typeof(string));
            for (int i = 0; i < 5; i++)
            {
                DateTime time = StartDate.AddDays(i);
                DataRow dr = dt.NewRow();
                dr["day"] = GetWeekDay(time.DayOfWeek);
                dr["time"] = time.ToString("yyyy-MM-dd");
                dt.Rows.Add(dr);

            }


            rpList.DataSource = dt;
            rpList.DataBind();
        }

        protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbName.Text = ddlRoom.SelectedItem.Text;
            BindData();
        }

        private void InitData()
        {

            lbName.Text = ddlRoom.SelectedItem.Text;
         
            FoodList = (from u in dishDataContext.Foods
                        where u.RoomID == int.Parse(ddlRoom.SelectedValue) && u.Time >= StartDate && u.Time <= StartDate.AddDays(4)
                        orderby u.OrderNum
                       
                        select new FoodModel
                        {
                            CreateTime = u.CreateTime.Value,
                            DinnerName = u.DinnerName,
                            Fruit = u.Fruit,
                            ID = u.ID,
                            Main = u.Main,
                            OrderNum = u.OrderNum,
                            Other = u.Other,
                            Price = u.Price,
                            Room = u.Room,
                            RoomID = u.RoomID.Value,
                            SideDish = u.SideDish,
                            Soap = u.Soap,
                            Time = u.Time.Value,
                            Type = u.Type,
                            Vegetable = u.Vegetable


                        }).ToList();

        }
        public List<FoodModel> FoodList
        {
            get
            {
                if (ViewState["FoodList"] == null)
                {
                    ViewState["FoodList"] = (from u in dishDataContext.Foods
                                             where u.RoomID == int.Parse(ddlRoom.SelectedValue) && u.Time >= StartDate && u.Time <= StartDate.AddDays(4)
                                             orderby u.OrderNum
                                             select new FoodModel
                                             {
                                                 CreateTime = u.CreateTime.Value,
                                                 DinnerName = u.DinnerName,
                                                 Fruit = u.Fruit,
                                                 ID = u.ID,
                                                 Main = u.Main,
                                                 OrderNum = u.OrderNum,
                                                 Other = u.Other,
                                                 Price = u.Price,
                                                 Room = u.Room,
                                                 RoomID = u.RoomID.Value,
                                                 SideDish = u.SideDish,
                                                 Soap = u.Soap,
                                                 Time = u.Time.Value,
                                                 Type = u.Type,
                                                 Vegetable = u.Vegetable


                                             }

                                             ).ToList();
                }
                return (List<FoodModel>)ViewState["FoodList"];
            }
            set
            {
                ViewState["FoodList"] = value;
            }
        }
        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemIndex >= 0)
            {
                FoodModel fd = FoodList.SingleOrDefault(l => l.Type == "早餐" && l.Time == StartDate.AddDays(e.Item.ItemIndex) && l.RoomID == int.Parse(ddlRoom.SelectedValue));
                if (fd != null)
                {
                    Literal tb1 = e.Item.FindControl("tbZC0") as Literal;
                    tb1.Text = fd.Main;

                    tb1 = e.Item.FindControl("tbZC1") as Literal;
                    tb1.Text = fd.SideDish;
                    tb1 = e.Item.FindControl("tbZC2") as Literal;
                    tb1.Text = fd.Vegetable;

                    tb1 = e.Item.FindControl("tbZC3") as Literal;
                    tb1.Text = fd.Soap;

                    tb1 = e.Item.FindControl("tbZC4") as Literal;
                    tb1.Text = fd.Fruit;

                    tb1 = e.Item.FindControl("tbZC5") as Literal;
                    tb1.Text = fd.Other;

                    tb1 = e.Item.FindControl("tbZC6") as Literal;
                    tb1.Text = fd.Price;


                }
                List<FoodModel> fd2 = FoodList.Where(l => l.Type == "午餐" && l.Time == StartDate.AddDays(e.Item.ItemIndex) && l.RoomID == int.Parse(ddlRoom.SelectedValue)).ToList();
                if (fd2.Count >= 6)
                {
                 
                    for (int j = 1; j < 7; j++)
                    {
                        fd = fd2[j - 1];
                        Literal tb1 = e.Item.FindControl("tbLun" + j.ToString()) as Literal;
                        tb1.Text = fd.DinnerName;

                        tb1 = e.Item.FindControl("tbLun1" + j.ToString()) as Literal;
                        tb1.Text = fd.Main;

                        tb1 = e.Item.FindControl("tbLun2" + j.ToString()) as Literal;
                        tb1.Text = fd.SideDish;
                        tb1 = e.Item.FindControl("tbLun3" + j.ToString()) as Literal;
                        tb1.Text = fd.Vegetable;

                        tb1 = e.Item.FindControl("tbLun4" + j.ToString()) as Literal;
                        tb1.Text = fd.Soap;

                        tb1 = e.Item.FindControl("tbLun5" + j.ToString()) as Literal;
                        tb1.Text = fd.Fruit;

                        tb1 = e.Item.FindControl("tbLun6" + j.ToString()) as Literal;
                        tb1.Text = fd.Other;

                        tb1 = e.Item.FindControl("tbLun7" + j.ToString()) as Literal;
                        tb1.Text = fd.Price;

                    }

                }
            }
        }
    }
}